/**
 * 离线音频录音视图。
 */

import QtQuick 2.0
import QtQuick.Window 2.0
import QtQuick.Controls 2.1
import com.yihu.xchat.util 1.0

import "../../js/dialog/AudioRecorderDialog.js" as Js

ApplicationWindow {
    id: recorderWindow
    title: "离线录音"

    minimumWidth: 250
    minimumHeight: 300
    maximumWidth: 250
    maximumHeight: 300

    flags: Qt.Window | Qt.WindowStaysOnTopHint

    signal recordCompleted(string fileName)

    Column {
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.bottom: parent.bottom

        anchors.bottomMargin: 20
        spacing: 20

        Image{
            source: "qrc:/images/editor_audio.png"

            anchors.horizontalCenter: parent.horizontalCenter

            width: 64
            height: 64
        }

        Text {
            id: recordTime

            anchors.horizontalCenter: parent.horizontalCenter

            property string defaultText: "00:00.000"
            text: defaultText

            color: 'red'
            font.bold: true
            font.pointSize: 15
            font.family: "Monospace"    // 使用等宽字体，防止时间刷新时的闪烁
        }

        Row{
            Button {
                id: startStop

                text: "开始"

                onClicked: {
                    if(!AudioRecorderSingleton.recording){
                        AudioRecorderSingleton.start();
                        timer.start();
                        startStop.text = "Stop";
                    } else {
                        AudioRecorderSingleton.stop();
                        timer.stop();
                        startStop.text = "Start";

                        send.enabled = true;
                        playback.enabled = true;
                        timer.total = 0;
                    }
                }
            }

            Button {
                id: pause

                text: "暂停"
                enabled: AudioRecorderSingleton.recording || AudioRecorderSingleton.paused;

                onClicked: {
                    if(AudioRecorderSingleton.recording){
                        AudioRecorderSingleton.pause();
                        timer.stop();
                        pause.text = "Resume";
                    } else {
                        AudioRecorderSingleton.start();
                        timer.start();
                        pause.text = "Pause";
                    }
                }
            }

            Button {
                id: playback

                text: "回放"
                enabled: false

                onClicked: {
                    //console.debug("Playing audio file:" + AudioRecorderSingleton.fileName);
                    AudioPlayerSingleton.playAudioFile(AudioRecorderSingleton.fileName);
                }
            }
        }

        Button {
            id: send

            text: "发送"
            enabled: false

            width: recorderWindow.maximumWidth + 30

            onClicked: {
                recordCompleted(AudioRecorderSingleton.fileName);

                recorderWindow.visible = false;
                recordTime.text = recordTime.defaultText

                enabled = false;
                playback.enabled = false;
            }
        }
    }

    // 定时器
    Timer  {
        id: timer
        interval: 10
        repeat: true
        onTriggered: Js.timeChanged()

        property int total: 0
    }
}
